package com.zf.shuashua.advert;

import android.app.Activity;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.dhcw.sdk.BDAdvanceBaseAppNative;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.AdvertReport;
import com.zf.shuashua.manager.ActionThirdReportManager;
import com.zf.shuashua.utils.Util;

/**
 * 激励视频展示
 * 变现猫互动激励视频
 */
public class RewardByteBxmManager {

    private TTAdNative mTTAdNative;
    private TTRewardVideoAd mttRewardVideoAd;
    private String mCodeId = "945710785";

    private static RewardByteBxmManager instance;

    private RewardByteBxmManager() {
    }

    public static RewardByteBxmManager getInstance() {
        if (null == instance) {
            synchronized (RewardByteBxmManager.class) {
                if (null == instance) {
                    instance = new RewardByteBxmManager();
                }
            }
        }
        return instance;
    }

    public void load(final Activity activity, final BDAdvanceBaseAppNative appNative) {
        this.mCodeId = getCodeId();
        //step1:初始化sdk
        TTAdManager ttAdManager = TTAdManagerHolder.get();
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(activity);
        //step3:创建TTAdNative对象,用于调用广告请求接口
        mTTAdNative = ttAdManager.createAdNative(ShuaApplication.getContext());

        loadAd(mCodeId, appNative);
    }

    public void play(Activity activity) {
        if (mttRewardVideoAd != null) {
            mttRewardVideoAd.showRewardVideoAd(activity);
        }
    }

    private void reportRequestStart(){
        ActionThirdReportManager.getInstance().reportRequestStart(11, 2, mCodeId, 1);
    }

    private void reportRequestSucc(int return_num) {
        ActionThirdReportManager.getInstance().reportRequestSucc(11, 2, mCodeId, return_num);
    }

    private void reportRequestFail(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestFail(11, 2, mCodeId, 1, error_code, error_msg);
    }

    private void reportRequestRend(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestRend(11, 2, mCodeId, error_code, error_msg);
    }

    private String uuiThirdAdvert;

    private void loadAd(final String codeId, final BDAdvanceBaseAppNative appNative) {
        uuiThirdAdvert = Util.getUUID32();
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId)
                .setSupportDeepLink(true)
                //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景，只要设置的值大于0即可
                .setExpressViewAcceptedSize(500, 500)
                .setUserID(Store.getInstance().getuserId())//用户id,必传参数
                .setMediaExtra("media_extra") //附加参数，可选
                .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        //step5:请求广告
        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                if(appNative != null){
                    appNative.onError(code);
                }
                reportRequestFail(code + "", message);
            }

            //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
            @Override
            public void onRewardVideoCached() {

            }

            //视频广告的素材加载完毕，比如视频url等，在此回调后，可以播放在线视频，网络不好可能出现加载缓冲，影响体验。
            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
                if(ad == null){
                    appNative.onError(0);
                }else{
                    bindRewardListener(ad, appNative);
                    appNative.onADLoad();
                    reportRequestSucc(1);
                }
            }
        });
        reportRequestStart();
    }

    private void bindRewardListener(TTRewardVideoAd ad, final BDAdvanceBaseAppNative appNative) {
        mttRewardVideoAd = ad;
        String planId = System.identityHashCode(ad) + "";
        mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                appNative.onADShow();
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoStart, getReportType(), "11");
            }

            @Override
            public void onAdVideoBarClick() {
                appNative.onADClick();
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoClick, getReportType(), "11");
            }

            @Override
            public void onAdClose() {
                appNative.onADClose();
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoEnd, getReportType(), "11");
            }

            @Override
            public void onVideoComplete() {
                appNative.onVideoComplete();
            }

            @Override
            public void onVideoError() {
                appNative.onError(0);
                reportRequestRend("10", "video error");
            }

            //视频播放完成后，奖励验证回调，rewardVerify：是否有效，rewardAmount：奖励梳理，rewardName：奖励名称
            @Override
            public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
                appNative.onReward();
            }

            @Override
            public void onSkippedVideo() {
                appNative.onSkipped();
            }
        });
    }

    private String getReportType() {
        return ReportConstants.REWARD_TYPE_HDM;
    }

    private String getCodeId() {
        return "945710785";
    }

}
